﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using DataProcessor.EF;
using Hjt212Parser;
using log4net;


namespace DataProcessor.Process
{
    public class ProcessorStatus
    {
        private static readonly ILog Log = LogManager.GetLogger(typeof(ProcessorStatus));

        public static void ProcessOffline(string content)
        {
            try
            {
                using (var contextTarget = new PlatformPhContext())
                {
                    var mn = Parser.GetStringByKey("MN", content);
                    if (string.IsNullOrEmpty(mn)) return;
                    var info = Util.GetInfoFromMn(mn);
                    if (info == null)
                    {
                        Log.Warn("MN：" + mn + " 找不到对应企业！");
                        return;
                    }
                    data_online_latest online_Latest = contextTarget.data_online_latest.Where(t => t.ps_code.Equals(info.ps_code)).OrderByDescending(t => t.online_state_time)
                           .FirstOrDefault(t => t.pi_code.Equals(info.code));

                    if (online_Latest == null)
                    {
                        online_Latest = new data_online_latest
                        {
                            ps_code = info.ps_code,
                            pi_code = info.code
                        };
                        contextTarget.data_online_latest.Add(online_Latest);
                    }
                    online_Latest.online_state = "离线";
                    online_Latest.online_state_time = Parser.GetDateTimeByKey("DataTime", content);
                    contextTarget.SaveChanges();

                    var online = new data_online
                    {
                        ps_code = info.ps_code,
                        pi_code = info.code,
                        online_state = "离线",
                        online_state_time = Parser.GetDateTimeByKey("DataTime", content)
                    };
                    contextTarget.data_online.Add(online);
                    contextTarget.SaveChanges();
                }
            }
            catch (Exception ex)
            {
                Log.Error(ex);
            }
        }
        public static void ProcessOnline(string content)
        {
            try
            {
                using (var contextTarget = new PlatformPhContext())
                {
                    var mn = Parser.GetStringByKey("MN", content);
                    if (string.IsNullOrEmpty(mn)) return;
                    var info = Util.GetInfoFromMn(mn);
                    if (info == null)
                    {
                        Log.Warn("MN：" + mn + " 找不到对应企业！");
                        return;
                    }
                    data_online_latest online_Latest = contextTarget.data_online_latest.Where(t => t.ps_code.Equals(info.ps_code)).OrderByDescending(t => t.online_state_time)
                           .FirstOrDefault(t => t.pi_code.Equals(info.code));

                    if (online_Latest == null)
                    {
                        online_Latest = new data_online_latest
                        {
                            ps_code = info.ps_code,
                            pi_code = info.code
                        };
                        contextTarget.data_online_latest.Add(online_Latest);
                    }
                    online_Latest.online_state = "在线";
                    online_Latest.online_state_time = Parser.GetDateTimeByKey("DataTime", content);
                    contextTarget.SaveChanges();
                }
            }
            catch (Exception ex)
            {
                Log.Error(ex);
            }
        }

    }
}
